package com.heima.skydemo.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.skydemo.auto.AutoFillCreate;
import com.heima.skydemo.auto.AutoFillUpdate;
import com.heima.skydemo.domin.common.PageResult;
import com.heima.skydemo.domin.common.Result;
import com.heima.skydemo.domin.dto.DishDTO;
import com.heima.skydemo.domin.dto.DishQueryPage;
import com.heima.skydemo.domin.vo.DishandFlavosVo;
import com.heima.skydemo.entity.Dish;
import com.heima.skydemo.entity.DishFlavor;
import com.heima.skydemo.mapper.DishMapper;
import com.heima.skydemo.service.ICategoryService;
import com.heima.skydemo.service.IDishFlavorService;
import com.heima.skydemo.service.IDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜品 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-04-15
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {
    @Autowired
    private IDishService dishService;
    @Autowired
    private IDishFlavorService dishFlavorService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private DishMapper dishMapper;

    @PutMapping("")
    @AutoFillUpdate
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品：{}",dishDTO);
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishService.updateById(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        QueryWrapper<DishFlavor> wrapper = new QueryWrapper<DishFlavor>().eq("dish_id", dish.getId());
        //查询该菜品口味是否为空
        List<DishFlavor> list = dishFlavorService.list(wrapper);
        for (DishFlavor dishFlavor : flavors) {
            dishFlavor.setDishId(dish.getId());
            if (CollectionUtils.isEmpty(list)){
                dishFlavorService.save(dishFlavor);
            }else {
                dishFlavorService.update(dishFlavor, wrapper);
            }
        }

        return Result.success();
    }
@GetMapping("/page")
    public Result<PageResult> pageQuery(DishQueryPage dishQueryPage){
        log.info("分页查询菜品：{}",dishQueryPage);
        PageResult pageResult = dishService.pageQueryone(dishQueryPage);
        return Result.success(pageResult);
    }
    @GetMapping("/{id}")
    public Result<DishandFlavosVo> listbyid(@PathVariable Long id){
        log.info("根据菜品id查询菜品：{}",id);
        DishandFlavosVo vo = new DishandFlavosVo();
        Dish dish = dishService.getById(id);
        BeanUtils.copyProperties(dish,vo);
        QueryWrapper<DishFlavor> wrapper = new QueryWrapper<DishFlavor>().eq("dish_id", id);
        List<DishFlavor> flavor = dishFlavorService.list(wrapper);
        vo.setFlavors( flavor);

        return Result.success(vo);
    }
    @GetMapping("/list")
    public Result<List<Dish>> listbycategorty(Long categoryId){
        log.info("根据分类id查询菜品：{}",categoryId);
        List<Dish> list = dishService.lambdaQuery().eq(Dish::getCategoryId, categoryId).list();
        return Result.success(list);
    }
    @DeleteMapping("")
    public Result delete (@RequestParam ArrayList<Long> ids){
        log.info("删除菜品：{}",ids);
        dishService.removeByIds(ids);
        return Result.success();
    }
    @PostMapping("")
    @AutoFillCreate
     public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}",dishDTO);
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishService.save(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor dishFlavor : flavors) {
            dishFlavor.setDishId(dish.getId());
            dishFlavorService.save(dishFlavor);
        }
            return Result.success();
        }
        @PostMapping("/status/{status}")
    public Result status(@PathVariable("status")Integer status,Long id){
        log.info("修改菜品状态：{}",status);
           dishMapper.updateStatus(status,id);
            return Result.success();
        }




}
